package com.hmall.order.interceptor;




import com.hmall.order.context.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String userId = request.getHeader("authorization");
        log.error("使用 用户身份校验 ，当前用户为：{}",userId);
        if (!userId.equals("2")) {
            //不放行
            log.error("用户身份校验失败，当前用户为：{}",userId);
            return false;
        }

        //存入 线程局部变量
        BaseContext.setCurrentUserId(Long.parseLong(userId));
        log.error("用户身份校验 成功：{}",userId);
        //都没问题  放行
        return true;

    }
}
